//
//  WelcomeView.swift
//  Weibo(Swift)
//
//  Created by 阳 on 16/7/13.
//  Copyright © 2016年 阳. All rights reserved.
//

import UIKit

class WelcomeView: UIView {
    
    lazy var iconView : UIImageView = {
        let iconView = UIImageView()
        iconView.medium_SetImageWithURL(UserAccount.shareUserAccount.avatar_large!, placeHolder: "avatar_default_big")
        return iconView
    }()
    lazy var welcomeLable: UILabel = {
        let welcomeLable = UILabel(text: "\(UserAccount.shareUserAccount.screen_name)欢迎归来",textAlignment: .Center)
        
        return welcomeLable
    }()
    override init(frame: CGRect) {
        super.init(frame: screenBounds)
        backgroundColor = UIColor(patternImage: UIImage(named: "ad_background")!)
        setupUI()
    }
   
    
    override func didMoveToSuperview() {
        print(__FUNCTION__)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        print(__FUNCTION__)
       
    }
    override func didMoveToWindow() {
        iconView.snp_updateConstraints { (make) -> Void in
            make.centerY.equalTo(self).offset(-150)
        }
        
        //添加动画
        //添加弹力动画usingSpringWithDamping: 越小越弹
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            //以下代码执行后,会调用layoutSubviews
            self.layoutIfNeeded()
            }) { (_) -> Void in
                //动画完成之后把欢迎视图移除
                self.removeFromSuperview()
        }
    }
    func setupUI()
    {
        print(__FUNCTION__)
        addSubview(iconView)
        addSubview(welcomeLable)
        iconView.layer.cornerRadius = 45
        iconView.layer.masksToBounds = true
        
        self.iconView.snp_makeConstraints { (make) -> Void in
            make.width.height.equalTo(90)
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-70)
        }
        self.welcomeLable.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self).offset(10)
            make.centerX.equalTo(self)
            
        }
        layoutIfNeeded()
    }
}
